package com.yzr.util.converter;

import com.yzr.util.FileType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description 转换工厂
 * @author: yzr
 * @date: 2021/10/20 13:58
 **/

@Component
public class ConversionFactory {

    @Resource
    private final ApplicationContext context = new AnnotationConfigApplicationContext();

    /**
     * 通过枚举获取要创建的对象
     *
     * @param support 文件格式枚举
     * @return Conversion子类
     */
    public final Conversion getInstance(FileType.Support support) {
        if (null == support) throw new NullPointerException("不能为空");
        switch (support) {
            case OFFICE:
            case PDF:
                return context.getBean(OfficeConversion.class);
            case TEXT:
                return context.getBean(TextConversion.class);
            case IMAGE:
                return context.getBean(ImageConversion.class);
            case TIF:
                return context.getBean(TifConversion.class);
            default:
                throw new NullPointerException("无此类型");
        }
    }
}
